﻿using System;
using Fly.Data;

namespace FlyWebFrame.Model
{
    public class Admin:SqlServer
    {

        public Admin()
        {
            this.table = "admin";
            this.key = "Id";
        }
        public int Id;
        public string Name;
        public string Pass;
        public string Email;

        public static bool Logged()
        {
            try
            {
                return (bool)Fly.Web.Session.Get("Logged");
            }
            catch
            {
                return false;
            }
        }

        public Result Login()
        {
            Result result = this.Select(null,null,null,null);
            if (result.Status)
            {
                dynamic user = result.Data;
                int i = 0;
                for (i = 0; i < user.Length; i++)
                {
                    if (user[i].Name == this.Name && user[i].Pass == this.Pass)
                    {
                        Fly.Web.Session.Set("Logged", true);
                        Fly.Web.Session.Set("Name", user[i].Name);
                        return new Result(true, null, null);
                    }
                }
                return new Result(false, "Tên đăng nhập hoặc mật khẩu sai!", null);
            }
            else
            {
                return new Result(false, "Tên đăng nhập hoặc mật khẩu sai!", null);
            }
        }

        public static void Logout()
        {
            Fly.Web.Session.Clear();
        }

        public bool Add()
        {
            return this.Insert("Name,Pass,Email").Status;
        }

        public bool IsNameExist(string Name)
        {
            this.Name = Name;
            return this.Select("Name = @Name", null, null, null).Status;
        }

        public bool Edit()
        {

            return this.Update("Name,Pass,Email").Status;
        }

        public bool ResetPass()
        {

            return this.Update("Pass").Status;
        }

        public bool Remove()
        {
            return this.Remove();
        }
        public Result Get()
        {
            return this.Select(null, null, null, null);
        }
        
    }
}